package com.lcredu.mapper;

import com.lcredu.entity.Monster;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author 黎长容
 * @date 2025-07-29 17:12
 * 注解形式
 */

/**
 * 使用注解的crud功能
 * 1.添加注解
 * 2.需要再mybatis配置文件中设置 <mapper class="com.lcredu.mapper.MonsterAnnotation"/>
 * 3.新增返回主键可以使用@Options
 * 4.如果实体类的id和数据库返回的id相同可以不用写,如果不相同就要写上
 *
 * @author 18442
 */
public interface MonsterAnnotation {
    //比如添加
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    @Insert(" insert into monster (age, birthday, email, gender, name, salary)\n" +
            "        VALUES (#{age}, #{birthday}, #{email}, #{gender}, #{name}, #{salary});")
    public void addMonster(Monster monster);

    //删除
    @Delete("delete from monster where id = #{id};")
    public void delMonster(Integer id);

    //修改
    @Update(" update monster\n" +
            "        set age = #{age},\n" +
            "            name=#{name}\n" +
            "        where id = #{id};")
    public void updateMonster(Monster monster);


    //查询单个
    @Select("select * from monster where id = #{id}")
    public Monster selectMonsterById(Integer id);

    //查询一个集合
    @Select("select * from monster")
    public List<Monster> selectMonsters();
}
